package com.gopay.bis.service.bank.impl.ebank;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.UmPayManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.UmPayConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.umpay.ebank.UmPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.MoneyUtil;
import com.gopay.remote.bank.BankInteractRemoteService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 联动优势网银直连业务实现类
 * 
 * @UmPayBankServiceImpl.java
 * @author zhengxue
 * @2013-12-4 上午9:45:24 www.gopay.com.cn Inc.All rights reserved.
 */
public class UmPayBankServiceImpl implements BankInteractRemoteService {
	@Resource(name = "umPayManager")
	UmPayManager umPayManager;

	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		UmPayMessage message = new UmPayMessage();
		BeanUtils.copyProperties(base, message);
		try {
			message = (UmPayMessage) umPayManager.sign(message);
			result.setDefaultModel(message);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}
	
	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		UmPayMessage message = new UmPayMessage();
		BeanUtils.copyProperties(base, message);
		try {
			UmPayMessage resMsg = (UmPayMessage) umPayManager.verify(message);
			message.setSign(resMsg.getSign()); // 将验签返回对象放入message中(响应联动优势信息)
			result.setModel("message", message);
			result.setDefaultModel(true);
			result.setSuccess(true);
			if (result != null && result.isSuccess() && (Boolean) result.getDefaultModel()) {
				String amount = base.getAmount().toString();
				amount = MoneyUtil.formatAmt(amount);
				Map<String, String> param = new HashMap<String, String>();
				if (message.getTradeState() != null && message.getTradeState().equals(UmPayConstants.TRADE_SUCCESS)) {
					// 如果该订单成功通知后台地址入账					
					param.put("orderId", base.getOrderId());
					param.put("amt", amount);
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "00");
					param.put("retCode", "00");
					param.put("retMsg", "");
					param.put("signData", message.getReqMap().toString());
					param.put("isFront", base.isFront()+"");
					result.setModel("isNotifyCashier", param);
					result.setOrderStatus(true);
				} else {
					// 通知后台地址该订单失败
					param.put("orderId", base.getOrderId());
					param.put("amt", amount);
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "01");
					param.put("retCode", "01");
					param.put("retMsg", "");
					param.put("signData", message.getReqMap().toString());
					param.put("isFront", base.isFront()+"");
					result.setModel("isNotifyCashier", param);
					result.setOrderStatus(false);
				}
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}

	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		UmPayMessage message = new UmPayMessage();
		BeanUtils.copyProperties(base, message);
		try {
			message = (UmPayMessage) umPayManager.query(message);
			result.setDefaultModel(message);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(base.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				String amount = base.getAmount().toString();
				amount = MoneyUtil.formatAmt(amount);
				param.put("orderId", base.getOrderId());
				param.put("amount", amount);
				if (message == null) {
					// 查询结果为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}

//				String retCode = message.getRetCode(); // 返回码
//				String retMsg = message.getRetMsg(); // 返回信息
//				String errorCode = message.getErrorCode(); // 交易错误码
				String tradeStatus = message.getTradeState(); // 订单状态
				//成功处理
				if (UmPayConstants.TRADE_SUCCESS.equals(tradeStatus)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
					param.put("returnMessage", "交易成功");
				}
				//失败处理
				else if (UmPayConstants.TRADE_FAIL.equals(tradeStatus)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
					param.put("returnMessage", "交易失败");
				}
				//不处理
				else if (UmPayConstants.TRADE_CANCEL.equals(tradeStatus)) {
					// 交易撤销
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行返回交易已撤销");
				} else if (UmPayConstants.TRADE_CLOSED.equals(tradeStatus)) {
					// 交易关闭
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行返回交易已关闭");
				} else if (UmPayConstants.WAIT_BUYER_PAY.equals(tradeStatus)) {
					// 交易等待付款
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行返回交易等待付款");
				} else {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行返回交易状态不明");
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}
}
